package com.wxproject.demo.Service.FackCheckService;
import	java.util.ArrayList;

import com.wxproject.demo.Utils.FaceUtil.GsonUtils;
import com.wxproject.demo.Utils.FaceUtil.HttpUtil;
import org.springframework.stereotype.Component;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zqf
 * @date 2021/4/1 21:56
 * @description
 */
@Component
public class FaceOperate {

    static String accessToken = AuthService.getAuth();
    //注册人脸信息
    public static String add(String userId, String groupId, String Url,String username) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", Url);
            map.put("group_id", groupId);
            map.put("user_id", userId);
            map.put("user_info", username);
            map.put("liveness_control", "NORMAL");
            map.put("image_type", "URL");
            map.put("quality_control", "NORMAL");
            String param = GsonUtils.toJson(map);
            String result = HttpUtil.post(url, accessToken, "application/json", param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //查询人脸信息
    public static String faceSearchByUrl(String Url, String groupId) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/search";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", Url);
            map.put("liveness_control", "NORMAL");
            map.put("group_id_list", groupId);
            map.put("image_type", "URL");
            map.put("quality_control", "NORMAL");
            String param = GsonUtils.toJson(map);
            String result = HttpUtil.post(url, accessToken, "application/json", param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    //查询人脸信息
    public static String faceSearchByImgToken(String Token, String groupId) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/search";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", Token);
            map.put("liveness_control", "NORMAL");
            map.put("group_id_list", groupId);
            map.put("image_type", "FACE_TOKEN");
            map.put("quality_control", "NORMAL");
            String param = GsonUtils.toJson(map);
            String result = HttpUtil.post(url, accessToken, "application/json", param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //活体检测
        public static String faceVerify(String Url) {
            // 请求url
            String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceverify";
            try {
                List<Map < String, Object>> res = new ArrayList<> ();
                Map<String, Object> map = new HashMap<>();
                map.put("image", Url);
                map.put("image_type", "URL");
                map.put("face_field","age,beauty,spoofing,expression");
                map.put("option","COMMON");
                res.add(map);
                String param = GsonUtils.toJson(res);
                String result = HttpUtil.post(url, accessToken, "application/json", param);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    public static String userGet(String userId) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/get";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("user_id", userId);
            map.put("group_id", "user");

            String param = GsonUtils.toJson(map);
            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String faceUpdate(String Url,String userId,String username) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/update";
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("image", Url);
            map.put("group_id", "user");
            map.put("user_id", userId);
            map.put("user_info", username);
            map.put("liveness_control", "NORMAL");
            map.put("image_type", "URL");
            map.put("quality_control", "NORMAL");
            String param = GsonUtils.toJson(map);
            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
//        FaceOperate.add("4","user","https://www.wwsszz.top/static/file/2021-04-02/2021-04-02-12-20-060d58e6922cda40618775c3e6d3823dd6.png");
//        FaceOperate.faceSearch("https://www.wwsszz.top/static/file/2021-04-02/2021-04-02-12-14-35abc81c843ec441bebc5bd899fa4a68d3.png","user");
    }
}
